<?php
namespace Admin\Model;
use Think\Model;
class JobModel extends Model{
    /**
     * @param $data
     * @param $jobId 工作表id
     */
    function isJobExist($data,$jobId = ""){
        $res = false;
        if($jobId){
            $where = "job='{$data['job']}' and id !=".$jobId;
        }else{
            $where = "job='{$data['job']}'";
        }
        $row = $this->where($where)->find();
        if(is_array($row)){
            $res = true;
        }
        return $res;
    }


    /**
     * @param $action 类别
     * @param $data 新增修改数据
     */
    function changeJobData($action,$data,$jobId = ""){
        if($jobId){
            $res =$this->where('id='.$jobId)->save($data);
        }else{
            $res =$this->add($data);
        }
        return $res;
    }

    /**
     * @param $id
     * @param $rows 表单数据
     *保存专业-职业表
     */
    function savePartJob($id,$rows){
        $partJobModel = D('partJob');
        $majorArray = $partJobModel->where("major_id=" . $id)->select();
        foreach ($rows as $k => $v) {
            foreach ($majorArray as $key => $val) {
                if ($v['id'] == $val['id']) {
                    $data = array();
                    if ($v['job_id']) {
                        $data['job_id'] = $v['job_id'];
                    }
                    if ($v['bili'] != $val['bili']) {
                        $data['bili'] = $v['bili'];
                    }
                    if (is_array($data) && count($data) >= 1) {
                        $result = $partJobModel->where("id=" . $v['id'])->save($data);
                    }
                }
            }
            if ($v['status']) {
                $data = array();
                $data['major_id'] = $id;
                $data['job_id'] = $v['job_id'];
                $data['bili'] = $v['bili'];
                $res = $partJobModel->add($data);
            }
        }
        return $res;
    }


}